﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AllenalaMente.State;

namespace AllenalaMente.Menu
{
    public partial class OptionsMenu : PhoneApplicationPage
    {
        private GameState gameState;
        private InsertName insertName;

        public OptionsMenu()
        {
            InitializeComponent();

            PageTitle.Text = Locale.Strings.Options;
            buttonChangeName.Content = Locale.Strings.ChangeName;
            buttonReset.Content = Locale.Strings.ResetGame;

            gameState = GameStateManager.GetState();

            //if (gameState.AudioOn)
            //{
            //    buttonAudio.Content = Locale.Strings.AudioOff;
            //    sliderVolume.Value = gameState.Volume;
            //    textBlockVolume.Text = "Volume" + " " + (int)gameState.Volume;
            //}
            //else
            //{
            //    buttonAudio.Content = Locale.Strings.AudioOn;
            //    sliderVolume.Value = 0.0;
            //    textBlockVolume.Text = "Volume";
            //}

            textBlockPlayerName.Text = gameState.playerName;

            Animation.FadeIn(this, 500);
        }

        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!gameState.AudioOn) return;

            int volume = (int)((Slider)sender).Value;
            gameState.Volume = (float)((Slider)sender).Value;

            //textBlockVolume.Text = "Volume";

            //if (volume != 0) textBlockVolume.Text += " " + volume;
        }

        private void buttonAudio_Click(object sender, RoutedEventArgs e)
        {
            gameState.AudioOn = !gameState.AudioOn;

            //if (gameState.AudioOn)
            //{
            //    buttonAudio.Content = Locale.Strings.AudioOff;
            //    sliderVolume.Value = gameState.Volume;
            //    textBlockVolume.Text = "Volume" + " " + (int)gameState.Volume;
            //}
            //else
            //{
            //    buttonAudio.Content = Locale.Strings.AudioOn;
            //    textBlockVolume.Text = "Volume";
            //    sliderVolume.Value = 0.0;
            //}
        }

        private string playerName = "";

        private void buttonReset_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(Locale.Strings.LoseProgress, Locale.Strings.ResetGame, MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                MessageBox.Show(Locale.Strings.Done);

                playerName = gameState.playerName;

                //gameState = new GameState();
                GameStateManager.ResetGameState();

                gameState.playerName = playerName;
            }
        }

        private void buttonChangeName_Click(object sender, RoutedEventArgs e)
        {
            insertName = new InsertName();
            LayoutRoot.Children.Add(insertName);

            insertName.buttonOk.Click += buttonOk_Click;

            insertName.textBlockDescription.Text = Locale.Strings.ChangeName;
            insertName.textBoxName.Text = gameState.playerName;

            insertName.textBoxName.KeyUp += textBoxName_KeyUp;
        }

        void textBoxName_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter) buttonOk_Click(this, null);
        }

        void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            if (insertName.textBoxName.Text != "")
            {
                gameState.playerName = insertName.textBoxName.Text;
                LayoutRoot.Children.Remove(insertName);
                insertName.buttonOk.Click -= buttonOk_Click;

                textBlockPlayerName.Text = gameState.playerName;
            }
        }
    }
}